library(dplyr)
library(plotly)
# Access key token
Sys.setenv('MAPBOX_TOKEN' = 'pk.eyJ1IjoiYnJpbWEiLCJhIjoiY2ptYm5pbXZ6MDd1czNwcW10OHN4Y2theSJ9.SNmXpvkIL14Wn1ebhRr_ug')
aegypti %>%
  select(VECTOR, Y, X, YEAR, COUNTRY) %>% # selecting variables
  filter(YEAR == 2004) %>% # filter by year
  plot_mapbox(x = ~X, y =~Y, split = ~VECTOR, mode = "scattermapbox", hoverinfo = "name" ) %>% # scattemapbox
  layout( title = "Scatter plot of mosquitos 2004",
        mapbox = list(style = "light"),
        margin = list(r = 25, l = 25, b = 25, t = 25, pad = 0.5)
        )
# 2013 scatter plot of mosquitos
aegypti %>%
  select(VECTOR, Y, X, YEAR, COUNTRY) %>%
  filter(YEAR == 2013) %>%
  plot_mapbox(x = ~X, y =~Y, split = ~VECTOR, mode = "scattermapbox", hoverinfo = "name" ) %>%
  layout( title = "Scatter plot of mosquitos 2004",
        mapbox = list(style = "light"),
        margin = list(r = 25, l = 25, b = 25, t = 25, pad = 0.5)
        )
# compute z (no. of mosquitos detected per country); choropleth
aegypti %>%
  group_by(COUNTRY) %>%
  mutate(Z = n()) %>%
  plot_geo() %>%
  add_trace(
    z =~Z, name = 'Mosquito (Z)', color =~Z, color = "reds",
    locations =~COUNTRY_ID
  ) %>%
  layout(
    title = "Number of mosquitos detected per country",
    geo = list(
  projection = list(type = "equirectangular")
    )
  )
aegypti %>%
  group_by(COUNTRY) %>%
  mutate(Z = log(n())) %>%
  plot_geo() %>%
  add_trace(
    z =~Z, name = 'Mosquito (Z)', color =~Z, color = "reds",
    locations =~COUNTRY_ID
  ) %>%
  layout(
    title = "Number of mosquitos detected per country",
    geo = list(
  projection = list(type = "equirectangular")
    )
  )
aegypti %>%
  group_by(COUNTRY) %>%
  mutate(Z = log(n())) %>%
  plot_geo() %>%
  add_trace(
    z =~Z, name = 'Mosquito (Z)', color =~Z, color = "reds",
    locations =~COUNTRY_ID
  ) %>%
  layout(
    title = "Number of mosquitos detected per country",
    geo = list(projection = list(type = "conic equal area"))
  )
d <- aegypti %>%
  select(VECTOR, Y, X, YEAR, COUNTRY) %>%
  filter(YEAR == 2013., COUNTRY == "Brazil") %>%
  mutate(X1 = cut_interval(X, 100)) %>%
  mutate(Y1 = cut_interval(Y, 100)) %>%
  group_by(X1) %>%
  mutate(mx = mean(X)) %>%
  mutate(nx = n()) %>%
  group_by(Y1) %>%
  mutate(my = mean(Y)) %>%
  mutate(ny = n()) %>%
  mutate(N = ny + nx) %>%
  group_by(X1, Y1) %>%
  mutate(N2 = n()) %>%
  group_by(X, Y) %>%
  mutate(mean(X,Y))
 d2 <- aegypti %>%
 select(VECTOR, Y, X, YEAR, COUNTRY) %>%
 filter(YEAR == 2013., COUNTRY == "Brazil") %>%
 mutate(X1 = cut_interval(X, 100)) %>%
 mutate(Y1 = cut_interval(Y, 100)) %>% 
 group_by(X1,Y1) %>%
  summarise(mx = mean(X), my =mean(Y), N = n())
d2 %>%
  plot_mapbox( mode = "scattermapbox", hoverinfo = "name") %>%
  add_markers(x = ~mx, y = ~my, split =~N,size = ~N) %>%
  layout( title = "Scatter plot of mosquitos 2013",
        mapbox = list(style = "light"),
        margin = list(r = 25, l = 25, b = 25, t = 25, pad = 0.5)
        )